package xin.lhuo.we.server.dao;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;
import xin.lhuo.we.server.entity.Document;
import xin.lhuo.we.server.entity.Photo;

import java.util.Date;
import java.util.List;

/**
 * @author Lhuo
 * @date 2019/5/26 16:06
 */
@Mapper
@Component
public interface DocumentMapper {
    @Insert("insert into we_document (user_id,folder_id,`name`,`key`,mime_type,simple_type,size,last_modified_date,etag,create_time,update_time) " +
            "values(#{userId},#{folderId},#{name},#{key},#{mimeType},#{simpleType},#{size},#{lastModifiedDate},#{eTag},#{createTime},#{updateTime})")
    void saveDoc(Document document);

    @Select("select * from we_document where folder_id = #{folderId} order by id desc")
    @Results(id = "document",
            value = {
                    @Result(column = "id", property = "id", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
                    @Result(column = "user_id", property = "userId", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
                    @Result(column = "folder_id", property = "folderId", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
                    @Result(column = "name", property = "name", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "key", property = "key", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "mime_type", property = "mimeType", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "simple_type", property = "simpleType", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "size", property = "size", javaType = Long.class, jdbcType = JdbcType.INTEGER),
                    @Result(column = "etag", property = "eTag", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "last_modified_date", property = "lastModifiedDate", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP),
                    @Result(column = "description", property = "description", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "create_time", property = "createTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP),
                    @Result(column = "update_time", property = "updateTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP)
            })
    List<Document> getDocumentByFolderId(@Param("folderId") Integer folderId);

    @Select("select count(1) from we_document where folder_id = #{folderId}")
    int getDocumentCountByFolderId(@Param("folderId") Integer folderId);

    @Delete("delete from we_document where id = #{id}")
    void deleteById(@Param("id") Integer id);

    @Select("select count(1) from we_document where id = #{id} and user_id = #{userId}")
    int checkDocumentById(@Param("id") Integer id,@Param("userId") Integer userId);

    @Select("select count(1) from we_document where folder_id = #{folderId}")
    int checkDocumentByFolderId(@Param("folderId") Integer folderId);

    @Update("update we_document set name = #{name} where id = #{id}")
    void renameDocument(@Param("id") Integer id, @Param("name")String name);

    @Select("select count(1) from we_document where user_id = #{userId}")
    int countByUserId(Integer userId);

    @Select("select * from we_document where user_id = #{userId} order by id desc limit 9")
    @ResultMap(value = "document")
    List<Document> selectRecentlyDocumentByUserId(Integer userId);
}
